package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.UserRole;
import service.UserRoleService;

import java.util.List;

@Api(value = "角色管理", description = "系统角色相关接口")
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/role")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation(value = "查询所有角色", notes = "获取系统所有角色信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回角色列表", response = UserRole.class, responseContainer = "List")
    })
    @GetMapping("/findAll")
    public ResponseEntity<List<UserRole>> findAll() {
        return ResponseEntity.ok(userRoleService.findAll());
    }

    @ApiOperation(value = "根据ID查询角色", notes = "通过role_id获取单个角色信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回角色", response = UserRole.class),
            @ApiResponse(code = 404, message = "未找到对应角色")
    })
    @GetMapping("/findById/{roleId}")
    public ResponseEntity<UserRole> findById(@PathVariable String roleId) {
        UserRole role = userRoleService.findById(roleId);
        return role != null ? ResponseEntity.ok(role) : ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "新增角色", notes = "创建一个新的系统角色")
    @ApiResponses({
            @ApiResponse(code = 201, message = "角色创建成功"),
            @ApiResponse(code = 400, message = "无效的角色数据"),
            @ApiResponse(code = 409, message = "角色ID已存在")
    })
    @PostMapping("/addRole")
    public ResponseEntity<Void> addRole(@RequestBody UserRole role) {
        // 检查角色ID是否已存在
        if (userRoleService.findById(role.getRoleId()) != null) {
            return ResponseEntity.status(HttpStatus.CONFLICT).build();
        }
        userRoleService.addRole(role);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(value = "更新角色信息", notes = "根据角色ID更新信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "角色信息更新成功"),
            @ApiResponse(code = 404, message = "未找到对应角色")
    })
    @PostMapping("/updateRole")
    public ResponseEntity<Void> updateRole(@RequestBody UserRole role) {
        int success = userRoleService.updateRole(role);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "删除角色", notes = "根据角色ID删除记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "角色删除成功"),
            @ApiResponse(code = 404, message = "未找到对应角色"),
            @ApiResponse(code = 409, message = "角色下存在用户，无法删除")
    })
    @DeleteMapping("/deleteRole/{roleId}")
    public ResponseEntity<Void> deleteRole(@PathVariable String roleId) {
        // 检查角色下是否存在用户（需调用用户服务）
        // 此处简化处理，实际项目中需添加关联检查

        int success = userRoleService.deleteRole(roleId);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }
}